<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>产品管理</el-breadcrumb-item>
      <el-breadcrumb-item>产品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 搜索查询 queryParam-->
      <el-row :gutter="20">
        <el-col :span="14">
          <div style="display: flex !important;">
            <el-input placeholder="" v-model="queryParam.name" style="width: 200px;margin-right: 5px;">
              <template slot="prepend">产品名称</template>
            </el-input>
            <template>
              <el-select v-model="queryParam.category" clearable placeholder="请选择分类">
                <el-option v-for="item in productDictData.category" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
              </el-select>
            </template>
            <template>
              <el-select v-model="queryParam.status" clearable placeholder="请选择状态">
                <el-option v-for="item in productDictData.status" :key="item.value" :label="item.name" :value="item.value">
                </el-option>
              </el-select>
            </template>
          </div>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="handleSizeChange">查询</el-button>
          <el-button type="primary" @click="updateOrCreateProductClick('add')">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- table表格数据 start -->
      <template>
        <el-table :data="productDataList" style="width: 100%" max-height="570">
          <!-- 数据表 -->
          <el-table-column fixed prop="name" label="名称" width="150">
          </el-table-column>
          <el-table-column prop="thumbnail_path" label="图片" width="120">
            <template slot-scope="scope">
              <div class="demo-image__preview">
                <el-image style="width: 100px; height: 100px" :src="scope.row.thumbnail_path" :preview-src-list="[scope.row.thumbnail_path]">
                </el-image>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="code" label="编码" width="80">
          </el-table-column>
          <el-table-column prop="category_text" label="分类" width="90">
          </el-table-column>
          <el-table-column prop="new_price" label="新价" width="90">
          </el-table-column>
          <el-table-column prop="old_price" label="原价" width="90">
          </el-table-column>
          <el-table-column prop="status_text" label="状态" width="100">
          </el-table-column>
          <el-table-column prop="is_hot" label="热门" width="100">
            <template slot-scope="scope">
              <el-switch v-model="scope.row.is_hot" active-color="#13ce66" inactive-color="#ff4949">
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="is_recommend" label="推荐" width="100">
            <template slot-scope="scope">
              <el-switch v-model="scope.row.is_recommend" active-color="#13ce66" inactive-color="#ff4949">
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="tags" label="标签" width="140">
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="90">
            <!-- 操作 -->
            <template slot-scope="scope">
              <el-button size="mini" type="warning" icon="el-icon-edit" @click="updateOrCreateProductClick(scope.row.id)" circle></el-button>
              <el-button size="mini" type="danger" icon="el-icon-delete" @click="deleteProduct(scope.$index,scope.row.id)"
                circle></el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!-- table表格数据 end -->
      <!-- 分页  start-->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryParam.currentPage"
        :page-sizes="[10, 20, 30, 40]" :page-size="100" layout="total, sizes, prev, pager, next, jumper" :total="totalResult">
      </el-pagination>
      <!-- 分页  end> -->
    </el-card>
  </div>
</template>

<script>
  import {
    getProductListApi,
    deleteProductApi,
    getProductDictApi
  } from '../request/api.js'
  export default {
    data() {
      return {
        productDataList: [],
        productDictData: {}, // 产品分类
        totalResult: 0,
        queryParam: {
          category: null,
          name: '',
          status: null,
          currentPage: 1,
          pageSize: 10
        }
      }
    },
    created() {
      this.getProductList()
      this.getProductDict()
    },
    methods: {
      // 获取分类数据
      async getProductDict() {
        const {
          data: res
        } = await getProductDictApi()
        console.log(res)
        this.productDictData = res.data
      },
      // 获取列表数据
      async getProductList() {
        const {
          data: res
        } = await getProductListApi(this.queryParam)
        console.log(res)
        this.productDataList = res.data.dataList
        this.totalResult = res.data.totalResult
      },
      // 分页事件
      handleSizeChange(newSize) {
        this.queryParam.pageSize = newSize
        this.queryParam.currentPage = 1
        this.getProductList()
      },
      handleCurrentChange(newPage) {
        this.queryParam.currentPage = newPage
        this.getProductList()
      },
      async deleteProduct(index, id) {
        // 删除
        const data = await deleteProductApi(id)
        if (data.status === 200 && data.data.status) {
          this.$message.success('删除成功！')
          this.productDataList.splice(index, 1)
        } else {
          this.$message.success('删除失败！')
        }
      },
      updateOrCreateProductClick(productId) {
        // 跳转到编辑页
        this.$router.push({
          name: 'product_detail',
          query: {
            productId: productId
          }
        })
      }
    }
  }
</script>

<style>
</style>
